package com.key.service.impl;


import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.common.TranException;
import com.key.common.vo.AreaPageVO;
import com.key.common.vo.AreaVO;
import com.key.entity.CArea;
import com.key.entity.PArea;
import com.key.mapper.CAreaMapper;
import com.key.service.AreaService;
;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private CAreaMapper cAreaMapper;
    @Override
    public R findAllArea() {
        R r=new R();
        List<PArea> pAreas = cAreaMapper.selectAllPArea();
        List<AreaVO> areaVOs=new ArrayList<>();
        for (PArea pArea : pAreas) {
            List<CArea> cAreas = cAreaMapper.selectAllCArea(pArea.getPAreaName());
            AreaVO areaVO=new AreaVO(pArea.getId(),pArea.getPAreaId(),pArea.getPAreaName(),pArea.getPTopArea(),pArea.getPState(),cAreas);
            areaVOs.add(areaVO);
        }
        if (areaVOs.size()>0){
            r=R.ok();
            r.setData(areaVOs);
        }else {
            r=R.no();
        }
        return r;
    }

    @Override
    public R findAreaByPAreaName(AreaPageVO areaPageVO) {
        Integer currentPage = areaPageVO.getCurrentPage();
        Integer pageSize = areaPageVO.getPageSize();
        PArea pArea = cAreaMapper.selectPAreaByPAreaId(areaPageVO.getPAreaName());
        PageHelper.startPage(currentPage, pageSize);
        List<CArea> cAreas = cAreaMapper.selectAllCArea(pArea.getPAreaName());
        R r=new R();
        if (cAreas.size()>0){
            r=R.ok();
            r.setData(cAreas);
        }else {
            r.setCode(200);
            r.setMsg("无数据");
        }

        return r;
    }

    @Override
    @Transactional
    public R addPArea(PArea pArea) {
        R r=new R();
        try {
            PArea pArea1 = cAreaMapper.selectPAreaByPAreaName(pArea.getPAreaName());
            if (pArea1==null){
                int i = cAreaMapper.insertPArea(pArea);
                int j=cAreaMapper.insertCArea(pArea);
                return (i+j)>0?R.ok():R.no();
            }else {
                r.setCode(200);
                r.setMsg("已存在");
                return r;
            }

        }catch (Exception e){
            throw new TranException("添加失败");
        }
    }

    @Override
    public R addCArea(CArea cArea) {
        R r=new R();
        CArea cArea1 = cAreaMapper.selectCAreaByCAreaName(cArea.getCAreaName());
        if (cArea1==null){
            int i = cAreaMapper.addCArea(cArea);
            return i>0?R.ok():R.no();
        }else {
            r.setCode(200);
            r.setMsg("已存在");
            return r;
        }

    }

    @Override
    public R findAreaByCAreaName(String cAreaName) {
        R r=new R();
        CArea cArea = cAreaMapper.selectCAreaByCAreaName(cAreaName);
        if(cArea!=null){
            r=R.ok();
            r.setData(cArea);
        }else {
            r.setCode(200);
            r.setMsg("无符合条件数据");
        }

        return r;
    }

    @Override
    @Transactional
    public R disableArea(Integer id) {
        Map map=new HashMap();
        map.put("id",id);
        map.put("state",1);
        try {
            CArea cArea = cAreaMapper.selectCAreaById(id);
            if (!cArea.getCAreaName().equals(cArea.getCTopArea())){
                int i = cAreaMapper.disableCArea(map);

            }else {
                cAreaMapper.disableCArea(map);
                map.put("pAreaName",cArea.getCTopArea());
                cAreaMapper.disablePArea(map);
            }
            return R.ok();
        }catch (Exception e){
            throw new TranException("禁用失败");
        }

    }

    @Override
    @Transactional
    public R enableArea(Integer id) {
        Map map=new HashMap();
        map.put("id",id);
        map.put("state",0);
        try {
            CArea cArea = cAreaMapper.selectCAreaById(id);
            if (!cArea.getCAreaName().equals(cArea.getCTopArea())){
                int i = cAreaMapper.disableCArea(map);

            }else {
                cAreaMapper.disableCArea(map);
                map.put("pAreaName",cArea.getCTopArea());
                cAreaMapper.disablePArea(map);
            }
            return R.ok();
        }catch (Exception e){
            throw new TranException("解禁失败");
        }

    }

    @Override
    @Transactional
    public R deleteArea(List<Integer> ids) {
        CArea cArea = cAreaMapper.selectCAreaById(ids.get(0));
        PArea pArea= cAreaMapper.findPAreaByCTopArea(cArea.getCTopArea());
        int i = cAreaMapper.selectCIdByCTopArea(cArea.getCTopArea());

            try {
                for (Integer id : ids) {
                    if (id == i&&pArea!=null) {
                        cAreaMapper.deleteCAreaById(id);
                        cAreaMapper.deletePAreaById(pArea.getId());
                    } else {
                        cAreaMapper.deleteCAreaById(id);
                    }
                }
                return R.ok();
            } catch (Exception e) {
                throw new TranException("删除失败");
            }


    }

    @Override
    public R findCAreaById(Integer id) {
        CArea cArea = cAreaMapper.selectCAreaById(id);
        R r=R.ok();
        r.setData(cArea);
        return r;
    }

    @Override
    @Transactional
    public R updateArea(CArea cArea) {
        CArea cArea1 = cAreaMapper.selectCAreaById(cArea.getId());
        try {
            if (cArea1.getCTopArea().equals(cArea1.getCAreaName())){
                cAreaMapper.updateCArea(cArea);
                cAreaMapper.updatePArea(cArea);
            }else {
                cAreaMapper.updateCArea(cArea);
            }

            return R.ok();
        }catch (Exception e){
            throw new TranException("修改失败");
        }

    }
}
